<?php
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2019 南京RXThink工作室
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <rxthink.cn@gmail.com>
// +----------------------------------------------------------------------

namespace App\Http\Controllers;

use App\Models\AdminModel;
use App\Models\DepModel;
use App\Services\AdminService;
use Illuminate\Http\Request;

/**
 * 人员-控制器
 * @author zongjl
 * @date 2019/5/23
 * Class AdminController
 * @package App\Http\Controllers
 */
class AdminController extends Backend
{
    /**
     * 构造方法
     * AdminController constructor.
     * @param Request $request
     * @author zongjl
     * @date 2019/5/23
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->model = new AdminModel();
        $this->service = new AdminService();
    }

    /**
     * 添加或编辑
     * @return view
     * @author 牧羊人
     * @since 2020/8/28
     */
    public function edit()
    {
        // 获取上级部门
        $deptModel = new DepModel();
        $result = $deptModel->getChilds(0, true);
        if ($result) {
            $deptList = array();
            foreach ($result as $val) {
                $key = (int)$val['id'];
                $deptList[$key] = $val;
                $vlist = isset($val['children']) ? $val['children'] : [];
                if ($vlist) {
                    foreach ($vlist as &$v) {
                        $k = (int)$v['id'];
                        $v['name'] = "|--" . $v['name'];
                        $deptList[$k] = $v;
                        $clist = isset($v['children']) ? $v['children'] : [];
                        if ($clist) {
                            foreach ($clist as &$vt) {
                                $kt = (int)$vt['id'];
                                $vt['name'] = "|--|--" . $vt['name'];
                                $deptList[$kt] = $vt;
                            }
                        }
                    }
                }
            }
        }
        // 绑定数据
        view()->share("deptList", $deptList);
        return parent::edit(); // TODO: Change the autogenerated stub
    }

    /**
     * 重置密码
     * @return mixed
     * @since 2020/8/28
     * @author 牧羊人
     */
    public function resetPwd()
    {
        if (IS_POST) {
            $result = $this->service->resetPwd();
            return $result;
        }
    }
}
